package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCollect;
import com.cskaoyan.bean.MarketCollectExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.collection.AppletCollectListData;
import com.cskaoyan.bean.collection.AppletCollectListVO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/05/09 09:12
 *
 * @author liutao
 */
@Service
public class AppletCollectServiceImpl implements AppletCollectService{

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public AppletCollectListVO collectList(Integer id, Integer type, BaseParam baseParam) {
        List<AppletCollectListData> appletCollectListDatas = new ArrayList<>();

        Integer limit = baseParam.getLimit();
        Integer page = baseParam.getPage();
        PageHelper.startPage(page, limit);
        // 用户id找到market_collect，再找到market_goods或market_topic
        MarketCollectExample example = new MarketCollectExample();
        example.setOrderByClause("add_time" + " desc");
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        // if (type == 0) {
        //     criteria.andTypeEqualTo(type.byteValue());
        //     List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);
        //
        // } else if (type == 1) {
        //     criteria.andTypeEqualTo(type.byteValue());
        // }

        // type == 0
        criteria.andTypeEqualTo(type.byteValue());
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);
        for (MarketCollect marketCollect : marketCollects) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketCollect.getValueId());
            AppletCollectListData appletCollectListData = new AppletCollectListData(marketGoods.getBrief(), marketGoods.getPicUrl(), marketCollect.getValueId(), marketGoods.getName(),
                    marketGoods.getId(), type, marketGoods.getRetailPrice());
            appletCollectListDatas.add(appletCollectListData);
        }
        PageInfo<AppletCollectListData> pageInfo = new PageInfo<>(appletCollectListDatas);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return AppletCollectListVO.data(total, pages, limit, page, appletCollectListDatas);
    }

    @Override
    public void collectAddordelete(Integer id, AppletCollectListData appletCollectListData) {
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andValueIdEqualTo(appletCollectListData.getValueId());
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);
        if (marketCollects.size() == 0) {
            MarketCollect marketCollect = new MarketCollect(null, id, appletCollectListData.getValueId(), appletCollectListData.getType().byteValue(), new Date(), new Date(), false);
            marketCollectMapper.insert(marketCollect);
            return;
        }
        // if (marketCollects.size() != 0) {
        //     MarketCollect marketCollect = marketCollects.get(0);
        //     marketCollect.setDeleted(true);
        //     MarketCollectExample marketCollectExample = new MarketCollectExample();
        //     MarketCollectExample.Criteria criteria1 = marketCollectExample.createCriteria();
        //     criteria1.andUserIdEqualTo(marketCollect.getId());
        //     int i = marketCollectMapper.updateByExampleSelective(marketCollect, marketCollectExample);
        // }
        MarketCollect marketCollect = marketCollects.get(0);
        marketCollect.setDeleted(!marketCollect.getDeleted());
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria1 = marketCollectExample.createCriteria();
        criteria1.andIdEqualTo(marketCollect.getId());
        int i = marketCollectMapper.updateByExampleSelective(marketCollect, marketCollectExample);
        return;
    }
}
